package com.soundnet.androidhelper.map;

import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.soundnet.androidhelper.R;
import com.soundnet.androidhelper.bean.CustomMarkerOptions;
import com.soundnet.androidhelper.callback.MapCallBack;


/**
 * Created by ljr on 2016/3/30.
 */
public class GaodeMap extends Map implements LocationSource, AMapLocationListener{
    private static final String TAG = "GaodeMap";

    private Activity mAct;
    private AMap mMap;
    private MapCallBack mMapCallBack;
    private OnLocationChangedListener mListener;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;
    private double mCurLat;
    private double mCurLng;



    public GaodeMap(Activity act, AMap map, MapCallBack mapCallBack) {
        this.mAct = act;
        this.mMap = map;
        this.mMapCallBack = mapCallBack;
    }

    @Override
    public void initMap() {
        super.initMap();
        if (mMap != null) {
            Log.d("mapactivity", "mapactivity init map not null");

            mMap.setLocationSource(this);
            mMap.setMyLocationEnabled(true);

            MyLocationStyle myLocationStyle = new MyLocationStyle();
            myLocationStyle.myLocationIcon(BitmapDescriptorFactory
                    .fromResource(R.mipmap.icon_cur_position));// 设置小蓝点的图标
            myLocationStyle.radiusFillColor(Color.argb(255, 255, 255, 255));
            //myLocationStyle.strokeWidth(0.0f);
            myLocationStyle.strokeColor(Color.argb(255, 255, 255, 255));
            mMap.setMyLocationStyle(myLocationStyle);

            mMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
            mMap.getUiSettings().setZoomControlsEnabled(false);
            mMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        }
    }

    /**
     * 激活定位
     */
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        Log.d(TAG, "activate");
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(mAct);
            mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mlocationClient.startLocation();
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
        Log.d(TAG, "deactivate");
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        Log.d(TAG, "onLocationChanged=" + aMapLocation.getLatitude());
        if (mListener != null) {
            mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
        }
        if (mMapCallBack != null && aMapLocation != null) {
            mMap.moveCamera(CameraUpdateFactory.zoomTo(15));
            mCurLat = aMapLocation.getLatitude();
            mCurLng = aMapLocation.getLongitude();
            mMapCallBack.onLocationChanged(mCurLat, mCurLng, aMapLocation.getAddress());

            if (aMapLocation.getLatitude() > 0 &&
                    aMapLocation.getLongitude() > 0) {
                deactivate();
            }
        }
    }

    @Override
    public void addMarker(CustomMarkerOptions customMarkerOptions) {
        super.addMarker(customMarkerOptions);
        Log.d(TAG, "addMarker");
        if (mMap != null && customMarkerOptions != null) {
            Log.d(TAG, "addMarker=" + customMarkerOptions.getmLatitude());
            MarkerOptions markerOption = new MarkerOptions();
            markerOption.title(customMarkerOptions.getmTitle());
            markerOption.position(new LatLng(customMarkerOptions.getmLatitude(), customMarkerOptions.getmLongitude()));
            markerOption.anchor(customMarkerOptions.getmAnchor_x(), customMarkerOptions.getmAnchor_y());
            markerOption.draggable(customMarkerOptions.ismDraggable());
            markerOption.zIndex(customMarkerOptions.getmIndex());
//            markerOption.icon(BitmapDescriptorFactory
//                    .fromResource(markerOptions.getmIconResourceId()));
            markerOption.icon(customMarkerOptions.getmIconBitmap());
            mMarkerList.add(mMap.addMarker(markerOption));
        }
    }

    @Override
    public void clearMarker() {
        super.clearMarker();
    }


}
